package algotithm.leetcode.bit.test1734;

/**
 * @author 25068
 * @Date 2021/5/11 9:13
 */
public class Solution {

    public static void main(String[] args) {
        int[] encoded = {3,1};
        Solution solution = new Solution();
        int[] decode = solution.decode(encoded);
        for (int i = 0; i < decode.length ; i++) {
            System.out.print(decode[i] + " ");
        }
    }

    public int[] decode(int[] encoded) {
        int res = 0;
        int ans = 0;
        // 实际上perm 的长度要比encoded的长度要多1
        int n = encoded.length;
        // 这个n对应的就是prem的前n个正整数
        n = n + 1;
        for (int i = 1; i <= n ; i++) {
            res ^= i;
        }
        for (int i = 1; i <= n - 1; i += 2) {
            ans ^= encoded[i];
        }
        int[] arr = new int[n];
        arr[0] = res ^ ans;
        for (int i = 1; i < n; i++) {
            arr[i] = encoded[i-1] ^ arr[i-1];
        }
        return arr;
    }
}
